package com.codesuc.aichat.userFavorite.controller;

import com.codesuc.aichat.userFavorite.entity.UserFavorite;
import com.codesuc.aichat.userFavorite.service.UserFavoriteService;
import com.codesuc.aichat.common.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
* @Author: ZhangGongMing
* @CreateTime: 2025/09/23 12:56
* @Description: UserFavorite控制层
* @Version: 1.0
*/
@RestController
@RequestMapping(value = "/api/v1/userFavorite")
@Tag(name = "UserFavoriteController", description = "UserFavorite控制层")
public class UserFavoriteController {

    @Autowired
    private UserFavoriteService userFavoriteService;

    /**
     * 用户新增收藏角色
     */
    @PostMapping("/add")
    @Operation(summary = "用户新增收藏角色")
    public Result<String> addUserFavoriteRole(
            @Parameter(description = "用户ID", required = true) @RequestParam Long userId,
            @Parameter(description = "角色ID", required = true) @RequestParam Long roleId) {
        boolean success = userFavoriteService.addUserFavoriteRole(userId, roleId);
        if (success) {
            return Result.ok("收藏角色成功");
        } else {
            return Result.error("收藏角色失败，可能已经收藏过该角色");
        }
    }

    /**
     * 用户删除收藏角色
     */
    @DeleteMapping("/remove")
    @Operation(summary = "用户删除收藏角色")
    public Result<String> removeUserFavoriteRole(
            @Parameter(description = "用户ID（可选，为空时使用当前登录用户ID）", required = false) @RequestParam(required = false) Long userId,
            @Parameter(description = "角色ID", required = true) @RequestParam Long roleId) {
        boolean success = userFavoriteService.removeUserFavoriteRole(userId, roleId);
        if (success) {
            return Result.ok("取消收藏成功");
        } else {
            return Result.error("取消收藏失败");
        }
    }

    /**
     * 分页查询用户收藏的角色
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询用户收藏的角色")
    public Result<Page<UserFavorite>> getUserFavoriteRolesPage(
            @Parameter(description = "用户ID", required = true) @RequestParam Long userId,
            @Parameter(description = "页码", required = false) @RequestParam(defaultValue = "1") Integer pageNo,
            @Parameter(description = "每页大小", required = false) @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<UserFavorite> favoritePage = userFavoriteService.getUserFavoriteRolesPage(userId, pageNo, pageSize);
        return Result.ok(favoritePage);
    }

    /**
     * 检查用户是否已收藏某角色
     */
    @GetMapping("/check")
    @Operation(summary = "检查用户是否已收藏某角色")
    public Result<Boolean> isUserFavoriteRole(
            @Parameter(description = "用户ID", required = true) @RequestParam Long userId,
            @Parameter(description = "角色ID", required = true) @RequestParam Long roleId) {
        boolean isFavorite = userFavoriteService.isUserFavoriteRole(userId, roleId);
        return Result.ok(isFavorite);
    }

}
